
var gs3 = require('gs3');
Authentication_Management = gs3.Modules.Authentication_Management;

var db_config = require('./db_config');

var userTable = {
    tableName: "user",
    primary: "id",
    username: 'Email',
    password: "Password",
    role: "Role_id"
};

var roleTable = {
    tableName: "role",
    primary: "id",
    role: "Role_Name"
};

var userTableSelection = ['id', 'Email', 'Name', 'Role_id'];

var Authentication_Impl = new Authentication_Management(db_config, userTable, roleTable, userTableSelection);

module.exports = Authentication_Impl;

//  Override Methods

Authentication_Impl.modifyUserOnLogin = function(user) {
    if (user) {
        user.Availability = 1;
        return user;
    }
};

Authentication_Impl.modifyUserOnLogout = function(user) {
    if (user) {        
        user.Availability = 0;
        return user;
    }
};

Authentication_Impl.onUnauthorizedAccess = function(req, res, reason) {
    if (reason === Authentication_Management.accessDenied.anonymous) {
        res.redirect('/login');
    }
};